<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/11/10
 * Time: 21:14
 */
namespace app\common\model;

use think\facade\View;
use think\facade\Validate;

class FormHelper {
    protected static $instance;
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($options);
        }
        return self::$instance;
    }

    /**
     * @param $field
     * @param $data
     * @return array
     */
    public function format($field, $data){
        if(is_string($field))
            $field = json_decode($field,true);

        if(is_string($field['configs']))
            $field['configs'] = json_decode($field['configs'],true);

        $funcname = 'format_'.$field['type'];
        if(is_callable(array(FormHelper::instance(),$funcname)))
        {
            $data = $this->$funcname($field,$data);
        }

        if( $field['isrequire'] && !isset($data[$field['name']])){
            return ['error'=>true,'msg'=>$field['title'].'不能为空'];
        }
        if($field['pattern']){
            $check = Validate::make()->rule($field['name'], $field['pattern'])->check($data);
            if($check == false)
            {
                return ['error'=>true,'msg'=>'字段“'.$field['title'].'”的值'.$field['errortip']];
            }
        }
        return ['error'=>false,'val'=>$data[$field['name']]];
    }

    public function format_date($field,$data){
        if(key_exists($field['name'],$data))
            $data[$field['name']] = strtotime( $data[$field['name']] );
        else
            $data[$field['name']] = time();
        return $data;
    }


    public function render($field,$model){
        if(is_string($field))
            $field = json_decode($field,true);
        $model[$field['name']] = isset($model[$field['name']])?$model[$field['name']]:'';

        if(is_string($field['configs']))
            $field['configs'] = json_decode($field['configs'],true);
        $funcname = 'render_'.$field['type'];

        if(is_callable(array(FormHelper::instance(),$funcname)))
        {
            return $this->$funcname($field,$model);
        }
        else{
            return View::fetch('common@items/'.$field['type'],['v'=>$field,'model'=>$model]);
        }
    }

    public function render_textbox($field,$model){
        $configs = $field['configs'];
        $configs['defaultval'] = isset($configs['defaultval'])?$configs['defaultval']:'';
        $configs['width'] = isset($configs['width'])?$configs['width']:'';
        $field['configs'] = $configs;
        $html = View::fetch('common@items/'.$field['type'],['v'=>$field,'model'=>$model]);
        return $html;
    }

    public function render_select($field,$model){
        $values = $field['configs']['values'];
        if($values){
            $values = explode(',',$values);
            $newvalues = array();
            foreach($values as $vv){
                $vv = explode('|',$vv);
                $newvalues[] = array('id'=>$vv[0],'val'=>$vv[1]);
            }
            $field['configs']['values'] = $newvalues;
        }
        $html = View::fetch('common@items/'.$field['type'],['v'=>$field,'model'=>$model]);
        return $html;
    }

    public function render_radio($field,$model){
        $values = $field['configs']['values'];
        if($values){
            $values = explode(',',$values);
            $newvalues = array();
            foreach($values as $vv){
                $vv = explode('|',$vv);
                $newvalues[] = array('id'=>$vv[0],'val'=>$vv[1]);
            }
            $field['configs']['values'] = $newvalues;
        }
        $html = View::fetch('common@items/'.$field['type'],['v'=>$field,'model'=>$model]);
        return $html;
    }

    public function render_checkbox($field,$model){
        $values = $field['configs']['values'];
        if($values){
            $values = explode(',',$values);
            $newvalues = array();
            foreach($values as $vv){
                $vv = explode('|',$vv);
                $newvalues[] = array('id'=>$vv[0],'val'=>$vv[1]);
            }
            $field['configs']['values'] = $newvalues;
        }
        $html = View::fetch('common@items/'.$field['type'],['v'=>$field,'model'=>$model]);
        return $html;
    }

    public function render_date($field,$model){
        $format = $field['configs']['format'];
        $format = preg_replace('/H{2,}/','H',$format);
        $format = preg_replace('/m{2,}/','i',$format);
        $format = preg_replace('/s{2,}/','s',$format);
        $format = preg_replace('/y{2,}/','Y',$format);
        $format = preg_replace('/M{2,}/','m',$format);
        $format = preg_replace('/d{2,}/','d',$format);

        $val = $model[$field['name']]?$model[$field['name']]:time();
        $val = date($format,$val);
        $model[$field['name']] = $val;
        $html = View::fetch('common@items/'.$field['type'],['v'=>$field,'model'=>$model]);
        return $html;
    }


    /*
     * 将数据库值换算成显示的字符串
     * */
    public function parse($field,$model){
        if(is_string($field))
            $field = json_decode($field,true);
        $model[$field['name']] = isset($model[$field['name']])?$model[$field['name']]:'';

        if(is_string($field['configs']))
            $field['configs'] = json_decode($field['configs'],true);

        $funcname = 'parse_'.$field['type'];
        if(is_callable(array(FormHelper::instance(),$funcname)))
        {
            return $this->$funcname($field,$model);
        }
        else{
            return $model[$field['name']];
        }
    }

    public function parse_date($field,$model){
        if(array_key_exists($field['name'],$model))
            return date('Y-m-d H:i:s',$model[$field['name']]);
        else
            return '';
    }

    public function parse_radio($field,$model){
        if(!array_key_exists($field['name'],$model))
            return '';

        $values = $field['configs']['values'];
        if($values){
            $values = explode(',',$values);
            $newvalues = array();
            foreach($values as $vv){
                $vv = explode('|',$vv);
                if($model[$field['name']] == $vv[0])
                    return $vv[1];
            }
        }
        return '';
    }

}